Passed
Pull Request — master (#216)
by
unknown
01:43
created

UpdateContactAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 33
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 31 2
1
import {
2
  Body,
3
  Controller,
4
  Inject,
5
  BadRequestException,
6
  UseGuards,
7
  Put,
8
  Param
9
} from '@nestjs/common';
10
import { AuthGuard } from '@nestjs/passport';
11
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
12
import { ICommandBus } from 'src/Application/ICommandBus';
13
import { UpdateContactCommand } from 'src/Application/Contact/Command/UpdateContactCommand';
14
import { ContactDTO } from '../DTO/ContactDTO';
15
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
16
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
17
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
18
import { UserRole } from 'src/Domain/HumanResource/User/User.entity';
19
20
@Controller('contacts')
21
@ApiTags('Contact')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'), RolesGuard)
24
export class UpdateContactAction {
25
  constructor(
26
    @Inject('ICommandBus')
27
    private readonly commandBus: ICommandBus
28
  ) {}
29
30
  @Put(':id')
31
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
32
  @ApiOperation({ summary: 'Update contact' })
33
  public async index(@Param() dto: IdDTO, @Body() contactDto: ContactDTO) {
34
    try {
35
      const { id } = dto;
36
      const {
37
        firstName,
38
        lastName,
39
        company,
40
        email,
41
        phoneNumber,
42
        notes
43
      } = contactDto;
44
45
      await this.commandBus.execute(
46
        new UpdateContactCommand(
47
          id,
48
          firstName,
49
          lastName,
50
          company,
51
          email,
52
          phoneNumber,
53
          notes
54
        )
55
      );
56
57
      return { id };
58
    } catch (e) {
59
      throw new BadRequestException(e.message);
60
    }
61
  }
62
}
63